package com.hyt.it.ogt.kq.service.gov.photo;

import com.hyt.exception.exception.BusinessLogicException;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.exception.GovInvalidRequestException;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.service.gov.model.dto.ProgressResult;
import com.hyt.it.ogt.kq.service.model.entity.Candidate;
import com.hyt.it.ogt.kq.service.gov.model.param.CandidateFileParam;
import com.hyt.progress.service.ProgressManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 照片模板类
 * @author huangyh
 * @since 2021-03-25
 */
@Component
public abstract class AbstractPhotoTemplate {

    @Autowired
    protected PhotoFactory photoFactory;

    /**
     * 单个上传
     * @param file 图片文件
     * @return 文件参数
     * @throws IOException 异常
     */
    public CandidateFileParam upload(MultipartFile file,String taskId) throws KqException, IOException {
        //校验图片
        this.checkPhoto(file);
        //上传图片
        return this.uploadPhoto(file,taskId);
    }

    /**
     * 批量导入照片
     * @param file 压缩包文件
     * @param taskId 考试任务id
     * @throws KqException 业务异常
     * @throws IOException io异常
     */
    @Transactional(rollbackFor = Exception.class)
    public void importPhoto(MultipartFile file, String taskId)  {
        try {
            List<Candidate> withoutPhotoList = new ArrayList<>();
            ProgressManager.updateProgressCurrent(8L, "文件接收成功");
            //校验批量导入图片文件
            Map<String, byte[]> result = this.checkBatchFile(taskId, file, withoutPhotoList);
            ProgressManager.updateProgressCurrent(38L, "文件验证成功");
            //批量上传导入的图片文件
            this.uploadBatch(result, taskId, withoutPhotoList);
            ProgressManager.finish(ProgressResult.builder().success(Boolean.TRUE).build());
        } catch (Exception e){
            BusinessLogicException.throwException(ResponseCode.ERROR_BATCH_IMPORT_CANDIDATE_PHOTO_EXCEPTION.getCode(),
                    ResponseCode.ERROR_BATCH_IMPORT_CANDIDATE_PHOTO_EXCEPTION.getMsg());
        }
    }

    /**
     * 校验单个图片文件
     * @param file
     * @throws GovInvalidRequestException 验证不通过 抛出异常
     * @throws IOException
     */
    protected abstract void checkPhoto(MultipartFile file) throws GovInvalidRequestException, IOException;

    /**
     * 上传单个图片文件
     * @param file
     * @param taskId
     * @return
     * @throws IOException
     */
    protected abstract CandidateFileParam uploadPhoto(MultipartFile file,String taskId) throws IOException;

    /**+
     * 校验批量文件
     * @param taskId
     * @param file
     * @return
     * @throws GovInvalidRequestException
     * @throws IOException
     */
    protected abstract Map<String, byte[]> checkBatchFile(String taskId, MultipartFile file, List<Candidate> withoutPhotoList) throws GovInvalidRequestException, IOException;

    /**
     * 批量上传导入的图片文件
     * @param result
     * @param taskId
     * @throws IOException
     */
    protected abstract void uploadBatch(Map<String, byte[]> result, String taskId, List<Candidate> withoutPhotoList) throws IOException;


}
